package com.wish.g1.house.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g1.common.core.domain.R;
import com.wish.g1.common.core.utils.StringUtils;
import com.wish.g1.common.core.web.controller.BaseController;
import com.wish.g1.common.core.web.domain.AjaxResult;
import com.wish.g1.common.core.web.page.TableDataInfo;
import com.wish.g1.common.log.annotation.Log;
import com.wish.g1.common.log.enums.BusinessType;
import com.wish.g1.common.security.annotation.RequiresPermissions;
import com.wish.g1.house.entity.Housing;
import com.wish.g1.house.entity.Region;
import com.wish.g1.house.service.IHousingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/housing")
public class HousingController extends BaseController {

    @Autowired
    IHousingService iHousingService;

    @Autowired
    RegionController regionController;


    /**
     * 查询楼盘列表
     */
    // @RequiresPermissions("house:housing:list")
    @GetMapping("/list")
    public TableDataInfo list(Housing housing)
    {
        startPage();
        QueryWrapper<Housing> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(housing.getHousingName())){
            wrapper.eq("housing_name", housing.getHousingName());
        }
        if(StringUtils.hasText(housing.getHousingAddress())){
            wrapper.like("housing_address", housing.getHousingAddress());
        }
        List<Housing> list = iHousingService.list(wrapper);
        List<Region> regions = regionController.getRegionList();
        Map<Integer, String> collect = regions.stream().collect(Collectors.toMap(Region::getId, Region::getName));
        for (Housing housing1 : list) {
            housing1.setSheng(collect.get(housing1.getRegionId1()));
            housing1.setShi(collect.get(housing1.getRegionId2()));
            housing1.setQu(collect.get(housing1.getRegionId3()));
        }
        return getDataTable(list);
    }

    /**
     * 新增楼盘
     */
    // @RequiresPermissions("house:housing:add")
    @Log(title = "楼盘", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Housing housing)
    {
        return toAjax(iHousingService.save(housing));
    }


}
